<template>
  <div id="active-container">
    <el-card>
      <template #header>
        <div class="clearfix">
          <span>
            <el-breadcrumb separator-class="el-icon-arrow-right">
              <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item>百叶管理</el-breadcrumb-item>
              <el-breadcrumb-item>活动管理</el-breadcrumb-item>
            </el-breadcrumb></span
          >
        </div>
      </template>
      <div>
        <el-button
          type="primary"
          size="small"
          @click="(drawerIsShow = true), (isCreate = true), (detialId = 0)"
          >创建活动</el-button
        >
        <el-select
          v-model="baiyemould"
          size="small"
          clearable
          placeholder="请选择百叶模块"
          style="width: 150px; margin-left: 15px"
          @change="baiyemouldChange"
        >
          <el-option
            v-for="item in baiyemoulds"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-select
          v-model="unexpired"
          size="small"
          clearable
          placeholder="请选择是否过期"
          style="width: 150px; margin-left: 15px"
          @change="unexpiredChange"
        >
          <el-option key="'true'" label="未过期" :value="'true'"></el-option>
          <el-option key="'false'" label="全部" :value="'false'"> </el-option>
        </el-select>
        <el-input
          placeholder="请输入内容"
          prefix-icon="el-icon-search"
          v-model="keyword"
          size="small"
          style="width: 250px; margin-left: 15px"
        >
        </el-input>
      </div>
      <el-table
        :data="actives"
        v-loading="loading"
        :height="height"
        :stripe="true"
        style="width: 100%; top: 15px"
      >
        <!-- <el-table-column type="selection" width="55" fixed="left">
        </el-table-column> -->
        <el-table-column label="活动名称" width="120" fixed="left">
          <template #default="scope"
            ><el-link
              type="primary"
              @click="editActive(scope.row.id)"
              href="javascript:;"
              >{{ scope.row.activityName }}</el-link
            ></template
          >
        </el-table-column>
        <el-table-column
          width="80"
          label="模块"
          prop="activityBlockValue"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          width="80"
          label="分数"
          prop="score"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          width="80"
          label="人数"
          prop="numberOfPeople"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="发布账户"
          prop="publisherTelephone"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          width="150"
          label="报名开始时间"
          prop="enrollStartTime"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          width="150"
          prop="enrollEndTime"
          label="报名结束时间"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="活动开始时间"
          width="150"
          prop="startTime"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="endTime"
          width="150"
          label="活动结束时间"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          show-overflow-tooltip
        ></el-table-column>

        <el-table-column label="操作" width="80" fixed="right">
          <template #default="scope">
            <el-popconfirm
              confirmButtonText="确定"
              cancelButtonText="取消"
              icon="el-icon-info"
              cancelButtonType="danger"
              iconColor="red"
              title="确定删除该活动信息吗？"
              @confirm="deleteActive(scope.row.id)"
            >
              <template #reference>
                <el-button type="danger" size="mini">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <div style="margin-top: 30px">
        <el-pagination
          @size-change="sizeChange"
          @current-change="pageChange"
          :current-page="page"
          :page-sizes="[15, 30, 50]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :disabled="loading"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
  <activeDrawer
    :isShow="drawerIsShow"
    :Id="detialId"
    :drawerClose="drawerClose"
    :isCreate="isCreate"
  />
</template>

<script lang="ts">
import { reactive, toRefs, watch, computed } from "vue";
import { Activity } from "@/api/baiye";
import Code from "@/consts/common";
import { ElMessage } from "element-plus";
import { useStore } from "vuex";
import activeDrawer from "./activeDrawer.vue";

export default {
  name: "Active",
  components: { activeDrawer },
  setup() {
    const store = useStore();
    const actives: Activity[] = [];
    let data = reactive({
      height: document.documentElement.clientHeight - 280,
      keyword: "",
      drawerIsShow: false,
      baiyemould: null,
      isCreate: true,
      detialId: 0,
      unexpired: null,
      actives: computed(() => store.state.Active.actives),
      loading: computed(() => store.state.Active.loading),
      total: computed(() => store.state.Active.total),
      page: computed(() => store.state.Active.page),
      size: computed(() => store.state.Active.size),
      baiyemoulds: [
        {
          value: 0,
          label: "博学",
        },
        {
          value: 1,
          label: "尽美",
        },
        {
          value: 2,
          label: "笃行",
        },
        {
          value: 3,
          label: "明德",
        },
      ],
      deleteActive(id: number) {
        const ids = [id];
        store.dispatch("Active/deleteActive", ids).then((res) => {
          if (res.code == Code.CODE_SUCCESS) {
            ElMessage({
              showClose: true,
              message: "删除成功",
              type: "success",
            });
            // @ts-ignore
            listActive(data.page, data.size, data.keyword, data.baiyemould);
          }
        });
      },
      baiyemouldChange(id: number) {
        // @ts-ignore
        data.baiyemould = id;
      },
      unexpiredChange(i: string) {
        if (i === "") {
          data.unexpired = null;
        } else {
          // @ts-ignore
          data.unexpired = i;
          console.log(data.unexpired);
        }
      },
      pageChange: (page: number) => {
        store.dispatch("Active/pageChange", {
          page: page,
          size: data.size,
          keyword: data.keyword,
          // @ts-ignore
          baiyemould: data.baiyemould,
          unexpired: Boolean(data.unexpired),
        });
      },
      drawerClose() {
        data.drawerIsShow = false;
      },
      sizeChange: (size: number) => {
        store.dispatch("Active/pageSizeChange", {
          page: data.page,
          size: size,
          keyword: data.keyword,
        });
      },
      editActive(id: number) {
        data.detialId = id;
        data.isCreate = false;
        data.drawerIsShow = true;
      },
    });
    const listActive = (
      page: number = 1,
      size: number = 15,
      keyword: string = "",
      // @ts-ignore
      activityBlock: number = null,
      unexpired: boolean = false
    ) => {
      store.dispatch("Active/listActive", {
        page,
        size,
        keyword,
        activityBlock,
        unexpired,
      });
    };
    listActive();
    watch(
      () => data.keyword,
      (newValue, oldVlaue) => {
        // @ts-ignore
        listActive(data.page, data.size, data.keyword, data.baiyemould);
      }
    );
    watch(
      () => data.baiyemould,
      (newValue, oldVlaue) => {
        // @ts-ignore
        listActive(data.page, data.size, data.keyword, data.baiyemould);
      }
    );
    watch(
      () => data.unexpired,
      (newValue, oldVlaue) => {
        let unexpired = null;
        if (data.unexpired !== "") {
          unexpired = data.unexpired === "true";
        }
        // @ts-ignore
        listActive(
          data.page,
          data.size,
          data.keyword,
          // @ts-ignore
          data.baiyemould,
          unexpired
        );
      }
    );
    const getHeight = () => {
      data.height = document.documentElement.clientHeight - 280;
    };
    window.addEventListener("resize", getHeight, false);
    return {
      ...toRefs(data),
    };
  },
};
</script>

<style lang="less" scoped>
</style>

<style lang="less">
.active-dialog {
  .el-card__body {
    padding: 5px 15px !important;
  }
  .el-dialog__body {
    padding: 20px 20px;
  }
}
</style>
